﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AddEpisode : System.Web.UI.Page
{
    ManageDatabase data;

    Episode episode;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            if (data == null)
            {
                data = new ManageDatabase();
            }
            if (!IsPostBack)
            {
                GetSeries();

                for (int i = 0; i < 3; i++)
                {
                    AddEpisodeLengthHours.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                for (int i = 0; i < 60; i++)
                {
                    AddEpisodeLengthMinutes.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    AddEpisodeLengthSeconds.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                Page.Form.Attributes.Add("enctype", "multipart/form-data"); 
            }
            episode = new Episode();
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }

    private void GetSeries()
    {
        ListItemCollection allSeries = data.GetSeriesSimple();
        AddEpisodeChooseSeries.Items.Clear();
        AddEpisodeChooseSeries.Items.Add(new ListItem("--Válasszon sorozatot--","0"));
        AddEpisodeChooseSeason.Items.Add(new ListItem("--Válasszon évadot--", "0"));
        foreach (ListItem item in allSeries)
        {
            AddEpisodeChooseSeries.Items.Add(item);
        } 
    }

    protected void AddEpisodeChooseSeries_SelectedIndexChanged(object sender, EventArgs e)
    {
        int series_id = Convert.ToInt32(AddEpisodeChooseSeries.SelectedValue);
        if (series_id == 0)
        {
            AddEpisodeChooseSeason.Items.Clear();
            AddEpisodeChooseSeason.Items.Add(new ListItem("--Válasszon évadot--", "0"));
        }
        else
        {
            ListItemCollection allSeason = data.GetSeasonsSimple(series_id);
            AddEpisodeChooseSeason.Items.Clear();
            AddEpisodeChooseSeason.Items.Add(new ListItem("--Válasszon évadot--", "0"));
            foreach (ListItem item in allSeason)
            {
                AddEpisodeChooseSeason.Items.Add(item);
            }
        }
    }

    protected void EpisodeCreate_Click(object sender, EventArgs e)
    {
        try
        {

            if (AddEpisodeChooseSeries.SelectedValue == "0")
            {
                Alert.Show("Kérem válasszon ki egy sorozatot!");
                return;
            }
            else
            {
                episode.seriesId = Convert.ToInt32(AddEpisodeChooseSeries.SelectedValue);
            }
            if (AddEpisodeChooseSeason.SelectedValue == "0")
            {
                Alert.Show("Kérem válasszon ki egy évadot!");
                return;
            }
            else
            {
                episode.seasonId = Convert.ToInt32(AddEpisodeChooseSeason.SelectedValue);
            }

            try
            {
                episode.episode = Convert.ToInt32(AddEpisodeNumber.Text);
            }
            catch (Exception)
            {
                Alert.Show("Az évad száma csak numerikus lehet!");
                return;
            }

            episode.titleHun = AddEpisodeTitleHun.Text;
            episode.titleOrig = AddEpisodeTitleOrig.Text;
            if (episode.titleHun == "" || episode.titleHun == " ")
            {
                Alert.Show("Kérem adja meg az epizód címét!");
                return;
            }

            if (AddEpisodeReleaseDate.Text != "")
            {
                try
                {
                    episode.releaseDate = DateTime.Parse(AddEpisodeReleaseDate.Text);
                }
                catch (Exception)
                {
                    Alert.Show("Érvénytelen a megjelenés dátuma!");
                    return;
                }
            }

            episode.length = (Convert.ToInt32(AddEpisodeLengthHours.SelectedValue) * 3600) +
                                (Convert.ToInt32(AddEpisodeLengthMinutes.SelectedValue) * 60) +
                                (Convert.ToInt32(AddEpisodeLengthSeconds.SelectedValue));


            if (AddEpisodeEmbedVideo.Text.IndexOf("&") == -1)
            {
                episode.videoUrl = AddEpisodeEmbedVideo.Text.Substring((AddEpisodeEmbedVideo.Text.IndexOf("v=") + 2), (AddEpisodeEmbedVideo.Text.Length - AddEpisodeEmbedVideo.Text.IndexOf("v=") - 2));
            }
            else
            {
                episode.videoUrl = AddEpisodeEmbedVideo.Text.Substring((AddEpisodeEmbedVideo.Text.IndexOf("v=") + 2), (AddEpisodeEmbedVideo.Text.IndexOf("&") - AddEpisodeEmbedVideo.Text.IndexOf("v=") - 2));
            }

            episode.description = AddEpisodeDescription.Text;

            episode.imgUrl = "";
            if (AddEpisodeImg.HasFile)
            {
                try
                {
                    if (AddEpisodeImg.PostedFile.ContentType == "image/pjpeg" || AddEpisodeImg.PostedFile.ContentType == "image/x-jpeg") //image/gif,image/x-png
                    {
                        if (AddEpisodeImg.PostedFile.ContentLength < 512000)
                        {
                            string filename = "episode_" + String.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + ".jpeg";
                            AddEpisodeImg.SaveAs(Server.MapPath("~/Pic/") + filename);
                            episode.imgUrl = filename;
                        }
                        else
                        {
                            StatusLabel.Text = "A maximális képméret 500 kb!";
                            return;
                        }
                    }
                    else
                    {
                        StatusLabel.Text = "Az alábbi képformátum nem támogatott: " + AddEpisodeImg.PostedFile.ContentType;
                        return;
                    }
                }
                catch (Exception)
                {
                    StatusLabel.Text = "Hiba történt a kép feltöltése során.";
                    return;
                }
            }

            Boolean success = data.InsertEpisode(episode, Functions.GetUserId(), User.IsInRole("admin"));

            if (!success)
            {
                Alert.Show("Hiba történt az éved létrehozása során!");
            }
            else
            {
                Response.Redirect("Series.aspx");
            }
        }
        catch (Exception)
        {
            Alert.Show("Hiba történt az éved létrehozása során!");
        }
    }

    protected void EpisodeCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Series.aspx");
    }

}